// 导入request.js请求工具
import request from '@/utils/request.js'

// 社情民意列表
export const getPublicOpinionsList = (params) => {
  return request.get('admins/publicOpinions/getAllPublicOpinions', { 
    params: {
      ...params,
      pageStatus: params.pageStatus // 确保 pageStatus 参数被传递
    }
  })
}

// 获取社情民意详情
export const getPublicOpinionDetails = (opinionId) => {
  return request.get(`/admins/publicOpinions/getPublicOpinionById/${opinionId}`)
}

// 修改社情民意状态
export const updatePublicOpinionStatus = ({ opinionId, status, isOr, pageStatus, department, handing_result, filePaths, fileName, size }) => {
  return request.put(`admins/publicOpinions/updatePublicOpinionStatus/${opinionId}`, null, {
    params: { 
      status,
      isOr,
      pageStatus,
      department,
      handing_result,
      filePaths,
      fileName,
      size
    }
  });
};

// 获取社情民意统计数据
export const getPublicOpinionStatistics = () => {
  return request.get('admins/publicOpinions/statistics')
}

// 获取社情民意类型分布
export const getPublicOpinionTypeDistribution = () => {
  return request.get('admins/publicOpinions/typeDistribution')
}

// 获取社情民意提交趋势
export const getPublicOpinionTrend = (startDate, endDate) => {
  const params = {}
  if (startDate) params.startDate = startDate
  if (endDate) params.endDate = endDate
  
  return request.get('admins/publicOpinions/trend', { params })
}

// 导出社情民意变化趋势数据
export const exportPublicOpinionTrendData = (startDate, endDate) => {
  const params = new URLSearchParams()
  if (startDate) params.append('startDate', startDate)
  if (endDate) params.append('endDate', endDate)
  
  return request({
    url: `/admins/publicOpinions/exportTrendData?${params.toString()}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  })
}

// 导出社情民意数据
export const exportPublicOpinions = (params) => {
  // 构建查询参数
  const queryParams = new URLSearchParams()
  
  // 添加所有参数
  if (params.pageStatus) queryParams.append('pageStatus', params.pageStatus)
  if (params.status && params.status !== 'all') queryParams.append('status', params.status)
  if (params.opinionType && params.opinionType !== 'all') queryParams.append('opinionType', params.opinionType)
  if (params.startDate) queryParams.append('startDate', params.startDate)
  if (params.endDate) queryParams.append('endDate', params.endDate)
  if (params.searchText) queryParams.append('searchText', params.searchText)
  if (params.department) queryParams.append('department', params.department)
  if (params.sort) queryParams.append('sort', params.sort)
  
  return request({
    url: `/admins/publicOpinions/exportPublicOpinions?${queryParams.toString()}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  })
}

// 获取社情民意部门列表
export function getPublicOpinionList() {
  return request({
    url: '/admins/getPubList',
    method: 'get'
  })
}

// 上传附件
export const uploadAttachment = (formData) => {
  return request({
    url: 'admins/uploadCoverImg',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 退回社情民意
export function sendBack(data) {
  return request({
    url: `/admins/publicOpinions/senBack/${data.opinionId}`,
    method: 'put',
    params: {
      status: data.status,
      isOr: data.isOr,
      pageStatus: data.pageStatus,
      department: data.department
    }
  })
}

// 更新公示状态
export const updatePublicStatus = ({ opinionId, status, isOr }) => {
  return request.put(`admins/publicOpinions/publicity/${opinionId}`, null, {
    params: { 
      status,
      isOr
    }
  });
};

// 下载文件
export const downloadFile = (url) => {
  return request({
    url: `/admins/publicOpinions/file/download`,
    method: 'get',
    timeout: 60000,
    params: { url },
    responseType: 'blob'  // 重要：指定响应类型为blob
  })
}
